const db = wx.cloud.database()
Page({
  data: {
    records: [],
    page: 0,
  },


  onLoad: function (options) {
    var phone = wx.getStorageSync('phone');
    console.log(phone)
    db.collection('order').where({
      celphone: parseInt(phone)
    }).orderBy('selectDate', 'desc').get().then(res => {
      this.setData({ records: res.data })
    })
  },

  deleteRecord: function (event) {
    var id = event.currentTarget.dataset.index
    var records = this.data.records
    var phone = wx.getStorageSync('phone');
    if (phone == records[id].adminphone || phone == records[id].celphone) {
      wx.cloud.callFunction({
        name: 'DeleteOrder',
        data: {
          id: records[id]._id,
        }
      }).then(res => {
        records.splice(id, 1)
        this.setData({
          records: records
        })
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 2000
        })
      }).catch(err => {
        // handle error
      })
    }
    else {
      wx.showToast({
        title: '您没有权限删除该预约',
        icon: 'success',
        duration: 2000
      })
    }
  },

  onReachBottom: function () {
    var page = this.data.page + 20
    var phone = wx.getStorageSync('phone');
    db.collection('order').where({
      celphone: parseInt(phone)
    }).orderBy('selectDate', 'desc').skip(page).get().then(res => {
      var records = this.data.records
      records = records.concat(res.data)
      this.setData({ records: records, page: page })
    })
  },


})